package com.gw.quotes.activity.fragment;

import java.util.HashMap;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.activity.AboutActivity;
import com.gw.quotes.activity.NoticeActivity;
import com.gw.quotes.activity.PriceInfoActivity;
import com.gw.quotes.adapter.GridAdapter;
import com.gw.quotes.entity.business.GetGoodsTopNEntity;
import com.gw.quotes.entity.business.GetGoodsTopNEntity.Pricedetail;
import com.gw.quotes.entity.business.Priceinfo;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GetGoodsTopNServiceImpl;
import com.gw.quotes.net.service.GetMapInfoImpl;
import com.gw.quotes.net.service.PriceinfoQueryServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.view.NoScrollGridView;
import com.yxw.view.YXWVerticalScrollView;

public class HomeFragment extends BaseMainFragment {

	private View contextView = null;
	private GridAdapter gridAdapter;

	@ViewInject(id = R.id.text_add_more, click = "onLoadMore")
	private TextView text_add_more;
	@ViewInject(id = R.id.layout_loading)
	private LinearLayout layout_loading;
	@ViewInject(id = R.id.gridView)
	private NoScrollGridView gridView;
	@ViewInject(id = R.id.notice_text, click = "noticeClick")
	private TextView noticeText;
	@ViewInject(id = R.id.text_introduction)
	private TextView text_introduction;
	@ViewInject(id = R.id.layout_introduction, click = "goIntroduction")
	private LinearLayout layout_introduction;
	@ViewInject(id = R.id.progress_introduction)
	private ProgressBar progress_introduction;

	@ViewInject(id = R.id.scrollview)
	private YXWVerticalScrollView scrollview;

	private static GetGoodsTopNEntity getGoodsTopNEntity;
	private HashMap<String, Object> mapIntr;

	private static int pageNo = 1;
	private static final int pageSize = 10;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (contextView == null) {
			contextView = inflater.inflate(R.layout.fragment_activity_home,
					null);
		}
		ViewGroup parent = (ViewGroup) contextView.getParent();
		if (parent != null) {
			parent.removeView(contextView);
		}
		FinalActivity.initInjectedView(this, contextView);
		initView();
		return contextView;
	}

	private void initView() {
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (getGoodsTopNEntity != null
						&& getGoodsTopNEntity.getPricelist() != null
						&& getGoodsTopNEntity.getPricelist().size() > id) {
					getPriceInfo(getGoodsTopNEntity.getPricelist()
							.get((int) id));
				}

			}
		});
		requestIntroduction();
		if (getGoodsTopNEntity == null) {
			dialog.show();
			requestTopN();
		} else {
			updateUI();
		}
	}

	// onClick 对公通知
	public void noticeClick(View v) {
		Intent intent = new Intent(contextView.getContext(),
				NoticeActivity.class);
		startActivity(intent);
	}

	// onClick 平台简介
	public void goIntroduction(View v) {
		Intent intent = new Intent(contextView.getContext(),
				AboutActivity.class);
		intent.putExtra("mapIntr", mapIntr);
		startActivity(intent);
	}

	/**
	 * 网络请求最新推荐商品
	 */
	private void requestTopN() {
		BaseServiceInter httpService = new GetGoodsTopNServiceImpl();
		Request request = new Request();
		request.setPageNo(pageNo);
		request.setPageSize(pageSize);
		httpService.handler(request, new HttpCallBack<GetGoodsTopNEntity>() {
			@Override
			public void onFinished(GetGoodsTopNEntity response) {

				// System.out.println("-----response中result的值:"
				// + response.getResult());

				Log.i("response中返回的result的值：", response.getResult()+"");

				if (response != null && response.getPricelist() != null
						&& response.getPricelist().size() > 0) {

					if (getGoodsTopNEntity == null
							|| getGoodsTopNEntity.getPricelist() == null) {
						getGoodsTopNEntity = response;
					} else {
						CommonTools.MergeArrayList(
								getGoodsTopNEntity.getPricelist(),
								response.getPricelist());
					}
					updateUI();
				} else {
					CommonTools.showShortToast(context, "网络请求失败！");
				}
				dialog.dismiss();
				text_add_more.setVisibility(View.VISIBLE);
				layout_loading.setVisibility(View.GONE);
			}
		});
	}

	/**
	 * 加载更多
	 */
	public void onLoadMore(View v) {
		text_add_more.setVisibility(View.GONE);
		layout_loading.setVisibility(View.VISIBLE);
		pageNo++;
		requestTopN();
	}

	/**
	 * 更新UI
	 */
	private void updateUI() {
		if (getGoodsTopNEntity != null) {

			if (gridAdapter == null) {
				gridAdapter = new GridAdapter(contextView.getContext(),
						R.layout.item_gridview,
						getGoodsTopNEntity.getPricelist());
				gridView.setAdapter(gridAdapter);
				scrollview.smoothScrollTo(0, 0);
			} else {
				gridAdapter.notifyDataSetChanged();
			}
		}
	}

	private void getPriceInfo(final Pricedetail pricedetail) {
		dialog.show();
		BaseServiceInter httpService = new PriceinfoQueryServiceImpl();
		Request request = new Request();
		request.setPriceguid(pricedetail.getPriceguid());
		Response response = httpService.handler(request,
				new HttpCallBack<Response<Priceinfo>>() {
					@Override
					public void onFinished(Response<Priceinfo> response) {
						dialog.dismiss();
						if (response.getResult() == 1) {
							Intent intent = new Intent(context,
									PriceInfoActivity.class);
							intent.putExtra("title", pricedetail.getGoodsname());
							intent.putExtra("priceinfo", response.getObj());
							startActivity(intent);
							// aCache.put(priceguid + "_priceinfo",
							// response.getObj());
						} else {
							CommonTools.showShortToast(context, "请求失败");
						}
					}
				});
	}

	/**
	 * 获取平台简介内容
	 */
	public void requestIntroduction() {
		progress_introduction.setVisibility(View.VISIBLE);
		text_introduction.setVisibility(View.GONE);
		BaseServiceInter httpService = new GetMapInfoImpl("getIndexMessage");
		Request request = new Request();
		Response response = httpService.handler(request,
				new HttpCallBack<Response<HashMap<String, Object>>>() {
					@Override
					public void onFinished(
							Response<HashMap<String, Object>> response) {
						if (response.getResult() == Response.RESULT_SUCCESS) {
							mapIntr = response.getObj();
							text_introduction.setText(mapIntr.get("content")
									.toString());
							progress_introduction.setVisibility(View.GONE);
							text_introduction.setVisibility(View.VISIBLE);
						} else {
							progress_introduction.setVisibility(View.GONE);
							CommonTools.showShortToast(context,
									response.getMsg());
						}
					}
				});
	}
}
